package cn.wolfcode.crud.service.impl;

import cn.wolfcode.crud.domain.Permission;
import cn.wolfcode.crud.mapper.PermissionMapper;
import cn.wolfcode.crud.qo.QuerObjcet;
import cn.wolfcode.crud.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PermissionImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> selectAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    //封装页面信息
    @Override
    public PageInfo<Permission> query(QuerObjcet qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> formList = permissionMapper.queryFormList(qo);
        return new PageInfo<Permission>(formList);
    }

    //处理业务
    @Autowired
    private ApplicationContext ctx;

    @Override
    public void reload() {
        //查询全部权限表达式
        List<String> permissions = permissionMapper.selectAllExpression();
        //  permissionMapper.selectall
        //从spring容器中获取获取所有的controller
        Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);
        //获取所有value值
        Collection<Object> values = beans.values();
        //遍历循环获取每一个controller
        for (Object controller : values) {
            //判断是否是代理类不是直接跳过
            if (!AopUtils.isCglibProxy(controller)) {
                continue;
            }
            //获取controller的字节码对象
            Class controllerClass = controller.getClass().getSuperclass();//拿父类的注解

            //获取controller的所有方法
            Method[] Methods = controllerClass.getDeclaredMethods();
            //遍历出每一个方法
            for (Method method : Methods) {
                //拿到所有注解
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);

                //判断是否有贴自定义的权限注解
                //如果有贴,封装成权限对象,并插入数据库
                if (annotation != null) {
                    //通过注解获取权限表达式    (方式一)
                    String expression = annotation.value()[0];
                    //通过反射拼接权限表达式    (方式二)
                    //判断权限表达式是否已经存在数据库中,如果不存在,就需要插入
                    if (!permissions.contains(expression)) {
                        Permission permission = new Permission();
                        permission.setName(annotation.value()[1]);
                        permission.setExpression(expression);
                        //添加方法
                        permissionMapper.insert(permission);
                    }

                }

            }

        }

    }

    @Override
    public List<String> selectByemp(Long id) {
        return permissionMapper.selectByemp(id);
    }
}
